﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TuongLaiDangCho.DAO;
using TuongLaiDangCho.DTO;

namespace TuongLaiDangCho.Enterface
{
    public partial class ucNote : DevExpress.XtraEditors.XtraUserControl
    {
        GhiChuDTO note = new GhiChuDTO();
        public ucNote()
        {
            InitializeComponent();
        }
        void LoadNote()
        {
            grcNote.DataSource=null;
            grcNote.DataSource = GhiChuDAO.LoadNote();            
        }

        private void ucNote_Load(object sender, EventArgs e)
        {
            LoadNote();            
        }

        private void grv_Note_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private void grv_Note_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            string s_Error = "";
            bool b_Vali=true;
            if (grv_Note.GetRowCellValue(e.RowHandle, "NoiDung").ToString() == "")
            {
                b_Vali = false;
                s_Error = "Bạn không thể bỏ trống mục này";
                LoadNote();
            }

            if (b_Vali)
            {
                if (e.RowHandle < 0)
                {
                    note.NoiDung = grv_Note.GetRowCellValue(e.RowHandle, "NoiDung").ToString();
                    note.ChuThich = grv_Note.GetRowCellValue(e.RowHandle, "ChuThich").ToString();
                    //XtraMessageBox.Show(note.NoiDung + "\n" + note.ChuThich + "\n");
                    GhiChuDAO.InsertNote(note);
                    LoadNote();
                }
                else
                {
                    note.NoiDung = grv_Note.GetRowCellValue(e.RowHandle, "NoiDung").ToString();
                    note.ChuThich = grv_Note.GetRowCellValue(e.RowHandle, "ChuThich").ToString();
                    note.ID=int.Parse(grv_Note.GetRowCellValue(e.RowHandle,"ID").ToString());
                    GhiChuDAO.UpdateNote(note);
                }
            }
            else
            {
                b_Vali = false;
                XtraMessageBox.Show(s_Error);
            }
        }

        private void grcNote_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && grv_Note.State != DevExpress.XtraGrid.Views.Grid.GridState.Editing)
            {
                if (XtraMessageBox.Show("Are You Sure?", "Quets", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    note.ID = int.Parse(grv_Note.GetRowCellValue(grv_Note.FocusedRowHandle, "ID").ToString());
                    GhiChuDAO.DeleteNote(note);
                    LoadNote();
                }
            }
        }


    }
}
